package com.bytecub.simulate.device.instance;

import com.bytecub.common.domain.DataResult;
import com.bytecub.common.domain.dto.PageReqDto;
import com.bytecub.common.domain.dto.PageResDto;
import com.bytecub.common.domain.dto.request.DevQueryReqDto;
import com.bytecub.common.domain.dto.response.device.DevicePageResDto;
import com.bytecub.common.domain.dto.response.prop.TemplateResDto;
import com.bytecub.common.metadata.ProductFuncTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
  * ByteCub.cn.
  * Copyright (c) 2020-2021 All Rights Reserved.
  * 
  * @author bytecub@163.com  songbin
  * @version Id: DeviceBoot.java, v 0.1 2021-01-07  Exp $$
  */
@Service
@Slf4j
public class DeviceBoot {
    @Value("#{'${bytecub.device.local.list:}'.split(';')}")
    private List<String> list;
    @Value("${bytecub.mqtt.server.url}")
    String mqttServerUrl;
    @Value("${bytecub.mqtt.client.timeout:30}")
    int    timeout;
    @Value("${bytecub.mqtt.client.keepalive:30}")
    int    keepAlive;
    @Value("${bytecub.device.data.mode:local}")
    String  mode;
    @Autowired
    com.bytecub.mqtt.domain.config.SimulateHttpProperties httpProperties;
    @Autowired
    DeviceDemoDataService                                            deviceDemoDataService;
    @Autowired
    HttpDeviceService httpDeviceService;
    List<DeviceInstance> deviceList;



    public void init(){
       switch (mode){
           case "local":
               this.deviceList = this.localGen();
               break;
           case "http":
               this.deviceList = httpDeviceService.httpGen();
               break;
           default:
               log.warn("模式【{}】配置错误", mode);
               System.exit(-1);
       }
//        for(int i = 0; i < deviceList.size(); i++){
//            reporterService.execute(deviceList.get(i), i);
//        }
    }

    private List<DeviceInstance> localGen(){
        List<DeviceInstance> deviceInstances = new ArrayList<>();
        TemplateResDto event = deviceDemoDataService.initProductDemo(httpProperties.getProduct(), ProductFuncTypeEnum.EVENT);
        TemplateResDto prop = deviceDemoDataService.initProductDemo(httpProperties.getProduct(), ProductFuncTypeEnum.PROP);
        for(String item:list){
            String[] device = item.split(",");
            String productCode = device[0];
            String deviceCode = device[1];
            String deviceSecret = device[2];
            DeviceInstance deviceInstance = new DeviceInstance(null,deviceCode, deviceSecret,
                    productCode, mqttServerUrl, event, prop);
            deviceInstance.genClient();
            deviceInstance.eventDemo = event;
            deviceInstance.propDemo = prop;
            deviceInstances.add(deviceInstance);
        }
        return deviceInstances;
    }

    public List<DeviceInstance> getDeviceList() {
        return deviceList;
    }
}
